package sar

import org.springframework.dao.DataIntegrityViolationException

class CourseCoordinatorController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [courseCoordinatorInstanceList: CourseCoordinator.list(params), courseCoordinatorInstanceTotal: CourseCoordinator.count()]
    }

    def create() {
        [courseCoordinatorInstance: new CourseCoordinator(params),userInstance: new User(params)]
    }

    def save() {
		def userInstance=new User(params).save(flush: true)
		def roleCoordinator=Role.findByAuthority("ROLE_COURSECOORDINATOR")
		new UserRole(user:userInstance,role:roleCoordinator).save(flush:true)
        def courseCoordinatorInstance = new CourseCoordinator(params)
		courseCoordinatorInstance.setUser(userInstance)
        if (!courseCoordinatorInstance.save(flush: true)) {
            render(view: "create", model: [courseCoordinatorInstance: courseCoordinatorInstance,userInstance:userInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'courseCoordinator.label', default: 'CourseCoordinator'), courseCoordinatorInstance.id])
        redirect(action: "show", id: courseCoordinatorInstance.id)
    }

    def show(Long id) {
        def courseCoordinatorInstance = CourseCoordinator.get(id)
        if (!courseCoordinatorInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'courseCoordinator.label', default: 'CourseCoordinator'), id])
            redirect(action: "list")
            return
        }

        [courseCoordinatorInstance: courseCoordinatorInstance]
    }

    def edit(Long id) {
        def courseCoordinatorInstance = CourseCoordinator.get(id)
        if (!courseCoordinatorInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'courseCoordinator.label', default: 'CourseCoordinator'), id])
            redirect(action: "list")
            return
        }

        [courseCoordinatorInstance: courseCoordinatorInstance]
    }

    def update(Long id, Long version) {
        def courseCoordinatorInstance = CourseCoordinator.get(id)
        if (!courseCoordinatorInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'courseCoordinator.label', default: 'CourseCoordinator'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (courseCoordinatorInstance.version > version) {
                courseCoordinatorInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'courseCoordinator.label', default: 'CourseCoordinator')] as Object[],
                          "Another user has updated this CourseCoordinator while you were editing")
                render(view: "edit", model: [courseCoordinatorInstance: courseCoordinatorInstance])
                return
            }
        }

        courseCoordinatorInstance.properties = params

        if (!courseCoordinatorInstance.save(flush: true)) {
            render(view: "edit", model: [courseCoordinatorInstance: courseCoordinatorInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'courseCoordinator.label', default: 'CourseCoordinator'), courseCoordinatorInstance.id])
        redirect(action: "show", id: courseCoordinatorInstance.id)
    }

    def delete(Long id) {
        def courseCoordinatorInstance = CourseCoordinator.get(id)
        if (!courseCoordinatorInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'courseCoordinator.label', default: 'CourseCoordinator'), id])
            redirect(action: "list")
            return
        }

        try {
            courseCoordinatorInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'courseCoordinator.label', default: 'CourseCoordinator'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'courseCoordinator.label', default: 'CourseCoordinator'), id])
            redirect(action: "show", id: id)
        }
    }
}
